from be.model import db_conn
from sqlalchemy.exc import SQLAlchemyError
from be.model import error
from be.model.store import New_Order
from sqlalchemy import and_

class Order(db_conn.DBConn):
    def __init__(self):
        db_conn.DBConn.__init__(self)

    def cancel_order(self, order_id):
        try:
            session=self.session
            row = session.query(New_Order).filter(
                and_(New_Order.order_id == order_id)).first()
            if row is None:
                return error.error_invalid_order_id(order_id)
            if row.status != 0:
                return error.error_invalid_order_id(order_id)

            row = session.query(New_Order).filter(
                and_(New_Order.order_id == order_id)).update({'status': -1})

            if row == 0:
                return error.error_invalid_order_id(order_id)

            session.commit()
        except SQLAlchemyError as e:
            return 528, "{}".format(str(e))
        except BaseException as e:
            return 530, "{}".format(str(e))

        return 200, "ok"
